<?php namespace system\tag;

use houdunwang\view\build\TagBase;

//echo 111;测试是否加载该文件
class Common extends TagBase
{

	/**
	 * 标签声明
	 * @var array
	 */
	public $tags = [
		'line' => ['block' => false],
		'category' => ['block' => true, 'level' => 4],
		'slide' => ['block' => true, 'level' => 4],
		'arclist' => ['block' => true, 'level' => 4],
	];

	//line 标签
	public function _line($attr, $content, &$view)
	{
		return 'link标签 测试内容';
	}

	//栏目标签
	public function _category($attr, $content, &$view)
	{
		$pid = isset($attr['pid']) ? $attr['pid'] : -1;
		//看解析成了什么，看编译文件
		//转义不转义变量
		$str = <<<str
				<?php
					\$db= Db::table('category');
					if($pid>=0){
						\$db->where('cate_pid',$pid);
					}
					\$data = \$db->get();
					foreach(\$data as \$field):
						\$field['url'] = __ROOT__ . '/c/' . \$field['cate_id'] . '.html';
				?>
					$content
				<?php
					endforeach
				?>
str;

		return $str;
	}

	//轮播图
	public function _slide($attr, $content, &$view)
	{
		//看解析成了什么，看编译文件
		//转义不转义变量
		$str = <<<str
				<?php
					\$db= Db::table('slide');
					\$data = \$db->get();
					foreach(\$data as \$field):
					    \$field['slide_thumb'] = __ROOT__ . '/' .  \$field['slide_thumb'] ;
				?>
					$content
				<?php
					endforeach
				?>
str;

		return $str;
	}

	//文章列表
	public function _arclist($attr, $content, &$view)
	{
		$cid = isset($attr['cid']) ? $attr['cid'] : -1;
		$thumb = isset($attr['thumb']) ? $attr['thumb'] : -1;
		$row= isset($attr['row']) ? $attr['row'] : -1;
		$arc_id= isset($attr['arc_id']) ? $attr['arc_id'] : -1;
		$str = <<<str
				<?php
					\$db= Db::table('article');
					if('$cid'>=0){
					    \$db->whereIn('cate_cid',explode(',','$cid'));
					}
					if('$arc_id'>=0){
					    \$db->whereIn('arc_id',explode(',','$arc_id'));
					}
					if($thumb==1){
					    \$db->where('arc_thumb','<>','');
					}
					if($row>=0){
						\$data = \$db->paginate($row);
					}else{
						\$data = \$db->get();
					}

					foreach(\$data as \$field):
					    \$field['arc_thumb'] = __ROOT__ . '/' .  \$field['arc_thumb'] ;
					    \$field['url'] = __ROOT__ . '/' .  \$field['arc_id'] . '.html' ;
				?>
					$content
				<?php
					endforeach
				?>
str;

		return $str;
	}
}